<?php
namespace Tongcheng\Controller;
use Think\Controller;
class NewsController extends CommonController {
    public function index(){
    	if(IS_AJAX){
            $model = M("News");

            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("last_time desc")->page("{$page},{$limit}")->select();
            foreach($res['list'] as $key=>$value){
                if(empty($value['cate_id'])){
                    $res['list'][$key]['cate_name'] = "无";
                }else{
                    $cate_name = M('news_cate')
                        ->where(array('id'=>$value['cate_id']))
                        ->getField('name');
                    $res['list'][$key]['cate_name'] = $cate_name;
                }

            }
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }

    public function add(){
        if(IS_AJAX){
            $model = M("News");
            $new['wid'] = session('wid');
            $new['title'] = I('post.title');
            $new['auther'] = I('post.auther');
            $new['pics'] = I('post.pics');
            $new['video_url'] = I('post.video_url');
            $new['content'] = $_POST['content'];
            $new['create_time'] = date("Y-m-d H:i:s");
            $new['cate_id'] = I('post.cate_id');

            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['auther'])){
                $this->error('请输入作者！');
            }
            if(empty($new['video_url']) && empty($new['content'])){
                $this->error('请输入视频链接或资讯内容！');
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $cateList = M('news_cate')
                ->where(array('wid'=>session('wid'),'status'=>1))
                ->select();
            $this->assign('news_cate',$cateList);
            $this->display();
        }
    }

    public function edit(){
        $model = M("News");
        $map['id'] = $_GET['id'];

        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['auther'] = I('post.auther');
            $new['pics'] = I('post.pics');
            $new['video_url'] = I('post.video_url');
            $new['content'] = $_POST['content'];
            $new['cate_id'] = I('post.cate_id');

            if(empty($new['title'])){
                $this->error('请输入标题！');
            }
            if(empty($new['auther'])){
                $this->error('请输入作者！');
            }
            if(empty($new['video_url']) && empty($new['content'])){
                $this->error('请输入视频链接或资讯内容！');
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();

            $pics = explode(",", $vo['pics']);
            $vo['pics_arr'] = "";
            foreach ($pics as $k => $v) {
                $vo['pics_arr'] .= "'{$v}',";
            }
            $cateList = M('news_cate')
                ->where(array('wid'=>session('wid'),'status'=>1))
                ->select();
            $this->assign('news_cate',$cateList);
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function top(){
        $model = M("News");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        $new['is_top'] = $_POST['status'];
        if($model->where($map)->save($new)){
            $this->success("置顶成功！");
        }else{
            $this->error("网络忙，请稍后再试！".$model->getLastSql());
        }
    }

    public function tui(){
        $model = M("News");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        $new['is_tui'] = $_POST['status'];
        $r = tui("news",$_POST['id'],session('wid'),$_POST['status']);
        if($model->where($map)->save($new) && $r){
            $this->success("推荐成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function del(){
        $model = M("News");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    public function cate(){
        if(IS_AJAX){
            $model = M("news_cate");
            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("add_time desc")->page("{$page},{$limit}")->select();
            foreach($res['list'] as $key=>$value){
                $time = date("Y-m-d H:i:s",$value['add_time']);
                $res['list'][$key]['add_time'] = $time;
            }
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }
    public function cateAdd(){
        if(IS_AJAX){
            $model = M("news_cate");
            $new['wid'] = session('wid');
            $new['name'] = I('post.name');
            $new['status'] = I('post.status');
            $new['add_time'] = time();

            if(empty($new['name'])){
                $this->error('请输入分类名称！');
            }
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }
    public function cateEdit(){
        $model = M("news_cate");
        $map['id'] = $_GET['id'];

        if(IS_AJAX){
            $new['name'] = I('post.name');
            $new['status'] = I('post.status');

            if(empty($new['name'])){
                $this->error('请输入标题！');
            }
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();

            $this->assign('vo',$vo);
            $this->display('cateAdd');
        }
    }
    public function cateDel(){
        $model = M("news_cate");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

}